package day16;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class Item1 {
    static Scanner sc = new Scanner(System.in);
    static List<Account> list=new ArrayList<>();
    static Map<String, List<String>> consumptionRecords = new HashMap<>();
    static class DataContainer implements Serializable {
        List<Account> list;
        Map<String, List<String>> consumptionRecords;
        public DataContainer(List<Account> list, Map<String, List<String>> consumptionRecords) {
            this.list = list;
            this.consumptionRecords = consumptionRecords;
        }
    }
public static void main(String[] args) throws IOException {
    loadData();
    one();
}
    public static void one() throws IOException {
        while(true){
            System.out.println("*************欢迎使用兜兜5G移动业务大厅*************");
            System.out.print("1.用户登录");
            System.out.print("2.用户注册");
            System.out.print("3.使用兜兜");
            System.out.print("4.话费充值");
            System.out.print("5.资费说明");
            System.out.println("6.退出系统");
            System.out.print("请选择:");
            int n=sc.nextInt();
            switch (n){
                case 1:login();break;
                case 2:reg();break;
                case 3:use();break;
                case 4:recharge();break;
                case 5:charge();break;
                case 6:return;
            }
        }
    }
    public static void two(Account a){
    while(true){
        System.out.println("*****兜兜5G移动用户菜单*****");
        System.out.println("1.本月账单查询");
        System.out.println("2.套餐余额查询");
        System.out.println("3.打印消费详单");
        System.out.println("4.套餐变更");
        System.out.println("5.办理退网");
        System.out.println("请选择(输入1-5选择功能,其他键返回上一级)");
        int n=sc.nextInt();
        switch (n){
            case 1:bill(a);break;
            case 2:;surplus(a);break;
            case 3:put(a);break;
            case 4:change(a);break;
            case 5:delete(a);return;
            default:return;
        }
    }
    }
    public static void loadData() {
        File file = new File("d:/io/data.ser");
        if (file.exists()) {
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
                DataContainer data = (DataContainer) ois.readObject();
                list = data.list;
                consumptionRecords = data.consumptionRecords;
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    public static void saveData() {
        File file = new File("d:/io/data.ser");
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/io/data.ser"))) {
            oos.writeObject(new DataContainer(list, consumptionRecords));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void surplus(Account a){
        System.out.println("******套餐余量查询******");
        System.out.println("您的卡号是"+a.number+",套餐内剩余:");
        System.out.println("通话时长:"+a.pack.calltime+"分钟");
        System.out.println("短信条数:"+a.pack.notenum+"条");
        System.out.println("上网流量:"+a.pack.netflow+"MB");
    }
    public static void bill(Account a){
        System.out.println("******本月账单查询******");
        System.out.println("您的卡号"+a.number+",当月账单:");
        System.out.println("套餐资费:"+a.pack.price+"元");
        System.out.println("合计:"+a.consumerMoney+"元");
        System.out.println("账户余额:"+a.balance+"元");
    }
    public static void put(Account a) {
        File directory = new File("D:/io");
        if (!directory.exists()) {
            directory.mkdirs(); // 创建多级目录
        }

        try (PrintStream ps = new PrintStream(new FileOutputStream("D:/io/put.txt"))) {

            ps.println("******" + a.number + "消费记录******");
            List<String> records = consumptionRecords.get(a.number);
            if (records == null || records.isEmpty()) {
                ps.println("没有消费记录");
                return;
            }
            ps.println("序号          类型        数据(通话(条)/上网(MB)/短信(条)");
            int count = 1;
            for (String record : records) {
                String type = "";
                String amount = "";
                if (record.contains("短信") || record.contains("信息")) {
                    type = "短信";
                    for (char c : record.toCharArray()) {
                        if (Character.isDigit(c)) {
                            amount += c;
                        }
                    }
                } else if (record.contains("通话")) {
                    type = "通话";
                    for (char c : record.toCharArray()) {
                        if (Character.isDigit(c)) {
                            amount += c;
                        }
                    }
                } else if (record.contains("流量")) {
                    type = "上网";
                    for (char c : record.toCharArray()) {
                        if (Character.isDigit(c)) {
                            amount += c;
                        }
                    }
                }
                ps.print(count + ".");
                for (int i = 0; i < 14 - String.valueOf(count).length(); i++) {
                    ps.print(" ");
                }
                ps.print(type);
                for (int i = 0; i < 12 - type.length(); i++) {
                    ps.print(" ");
                }
                ps.println(amount);
                count++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String file="D:/io/put.txt";
        try (BufferedReader bf=new BufferedReader(new FileReader(file))){
            String line;
            while((line=bf.readLine())!=null){
                System.out.println(line);
            }
        }catch (IOException e){
            System.out.println("读取错误");
        }
    }
    public static void change(Account a){
        System.out.println("******套餐变更******");
        System.out.println("1.话痨套餐 2.网虫套餐 3.超人套餐, 请选择套餐(输入序号):");
        int m=sc.nextInt();
        String newPackageType = "";
        boolean same = false;
        switch (m) {
            case 1: newPackageType = "话痨套餐"; same = (a.pack instanceof TallPackage); break;
            case 2: newPackageType = "网虫套餐"; same = (a.pack instanceof NetPackage);break;
            case 3: newPackageType = "超人套餐"; same = (a.pack instanceof SuperPackage);break;
        }
        if (same) {
            System.out.println("对不起，您已是该套餐用户，无需换套餐!");
            return;
        }
        Package p=null;
        switch (m){
            case 1:p=new TallPackage(58,500,0,30); break;
            case 2:p=new NetPackage(68,0,3072,0); break;
            case 3:p=new SuperPackage(78,200,1024,50); break;
        }
        if(a.balance-p.price<0){
            System.out.println("对不起,您的余额不足以支付新套餐月资费,请充值后再办理更换套餐业务!");
            return;
        }else {
            a.balance=a.balance-p.price;
            a.pack=p;
        }
        saveData();
    }
    public static void delete(Account a){
        System.out.println("******办理退网******");
        System.out.println("卡号"+a.number+"办理退网成功!");
        list.remove(a);
        System.out.println("谢谢使用!");
        saveData();
    }
    private static boolean addConsumptionRecord(String number, String record) {
        boolean isFirstRecord = false;

        if (!consumptionRecords.containsKey(number)) {
            consumptionRecords.put(number, new ArrayList<>());
            isFirstRecord = true;
        }

        consumptionRecords.get(number).add(record);

        return isFirstRecord;
    }
    public static void recharge(){
        System.out.println("请输入充值卡号");
        String number=sc.next();
        System.out.println("请输入充值金额");
        double money;
        while(true){
            money = sc.nextDouble();
            if(money<50){
                System.out.println("话费50元起充!");
            }else{
                break;
            }
        }
        for(Account a:list){
            if(number.equals(a.number)){
                a.balance=a.balance+money;
                System.out.println("充值成功,当前话费余额为"+a.balance+"元");
                saveData();
                return;
            }
        }
    }

    public static void charge(){
    String file="d:\\io\\charge.txt";
    try (BufferedReader bf=new BufferedReader(new FileReader(file))){
        String line;
        while((line=bf.readLine())!=null){
            System.out.println(line);
        }
    }catch (IOException e){
        System.out.println("读取错误");
    }
    }
    public static void use(){
    Random random=new Random();
        System.out.println("请输入手机号");
        String number=sc.next();
        for(Account a:list){
            if(number.equals(a.number)){
                int r=random.nextInt(6)+1;
                switch(r){
                    case 1:event1(a);break;
                    case 2:event2(a);break;
                    case 3:event3(a);break;
                    case 4:event4(a);break;
                    case 5:event5(a);break;
                    case 6:event6(a);break;
                }
            }
        }
    }
    public static void event1(Account a){
    int spend=50;
     if(a.pack.notenum-spend>=0){
         a.pack.notenum-=spend;
         System.out.println("通知朋友手机换号,发送信息50条");
         String record="发送信息50条";
         boolean isFirstRecord = addConsumptionRecord(a.number, record);
         if (isFirstRecord) {
             System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
         }
     }else {
       if((a.pack.notenum+10*a.balance)-spend>0){
           a.pack.notenum=0;
           System.out.println("通知朋友手机换号,发送信息50条");
           String record="发送信息50条";
           a.consumerMoney+=(spend-a.pack.notenum)*0.1;
           a.balance-=(spend-a.pack.notenum)*0.1;
           boolean isFirstRecord = addConsumptionRecord(a.number, record);
           if (isFirstRecord) {
               System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
           }
       }else {
           a.pack.notenum=0;
           int n=(int)((a.pack.notenum+10*a.balance));
           System.out.println("本次已发"+n+"条短信,您的余额不足,请充值后再使用!");
           a.consumerMoney+= a.balance;
           a.balance=0;
       }
     }
        saveData();
    }
    public static void event2(Account a){
        int spend=30;
        if(a.pack.calltime-spend>=0){
            a.pack.calltime-=spend;
            System.out.println("询问妈妈身体状况,本地通话30分钟");
            String record="本地通话30分钟";
            boolean isFirstRecord = addConsumptionRecord(a.number, record);
            if (isFirstRecord) {
                System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
            }
        }else {
            if((a.pack.calltime+5*a.balance)-spend>0){
                a.pack.calltime=0;
                System.out.println("询问妈妈身体状况,本地通话30分钟");
                String record="本地通话30分钟";
                a.consumerMoney+=(spend-a.pack.calltime)*0.2;
                a.balance-=(spend-a.pack.calltime)*0.2;
                boolean isFirstRecord = addConsumptionRecord(a.number, record);
                if (isFirstRecord) {
                    System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
                }
            }else {
                a.pack.calltime=0;
                int n=(int)((a.pack.calltime+5*a.balance));
                System.out.println("本次已通话"+n+"分钟,您的余额不足,请充值后再使用!");
                a.consumerMoney+= a.balance;
                a.balance=0;
            }
        }
        saveData();
    }
    public static void event3(Account a){
        int spend=400;
        if(a.pack.netflow-spend>=0){
            a.pack.netflow-=spend;
            System.out.println("和网友聊天,使用流量400mb");
            String record="使用流量400mb";
            boolean isFirstRecord = addConsumptionRecord(a.number, record);
            if (isFirstRecord) {
                System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
            }
        }else {
            if((a.pack.netflow+10*a.balance)-spend>0){
                a.pack.netflow=0;
                System.out.println("和网友聊天,使用流量400mb");
                String record="使用流量400mb";
                a.consumerMoney+=(spend-a.pack.netflow)*0.1;
                a.balance-=(spend-a.pack.netflow)*0.1;
                boolean isFirstRecord = addConsumptionRecord(a.number, record);
                if (isFirstRecord) {
                    System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
                }
            }else {
                a.pack.netflow=0;
                int n=(int)((a.pack.netflow+10*a.balance));
                System.out.println("本次已上网"+n+"分钟,您的余额不足,请充值后再使用!");
                a.consumerMoney+= a.balance;
                a.balance=0;
            }
        }
        saveData();
    }
    public static void event4(Account a) {
        int spend = 60;
        if (a.pack.notenum - spend >= 0) {
            a.pack.notenum -= spend;
            System.out.println("通知朋友手机换号,发送信息60条");
            String record = "发送信息60条";
            boolean isFirstRecord = addConsumptionRecord(a.number, record);
            if (isFirstRecord) {
                System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
            }
        } else {
            if ((a.pack.notenum + 10 * a.balance) - spend > 0) {
                a.pack.notenum = 0;
                System.out.println("通知朋友手机换号,发送信息60条");
                String record = "发送信息60条";
                a.consumerMoney += (spend - a.pack.notenum) * 0.1;
                a.balance-=(spend-a.pack.notenum)*0.1;
                boolean isFirstRecord = addConsumptionRecord(a.number, record);
                if (isFirstRecord) {
                    System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
                }
            } else {
                a.pack.notenum = 0;
                int n = (int) (a.pack.notenum + 10 * a.balance);
                System.out.println("本次已发" + n + "条短信,您的余额不足,请充值后再使用!");
                a.consumerMoney += a.balance;
                a.balance = 0;
            }
        }
        saveData();
    }

    public static void event5(Account a) {
        int spend = 40;
        if (a.pack.calltime - spend >= 0) {
            a.pack.calltime -= spend;
            System.out.println("询问妈妈身体状况,本地通话40分钟");
            String record = "本地通话40分钟";
            boolean isFirstRecord = addConsumptionRecord(a.number, record);
            if (isFirstRecord) {
                System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
            }
        } else {
            if ((a.pack.calltime + 5 * a.balance) - spend > 0) {
                a.pack.calltime = 0;
                System.out.println("询问妈妈身体状况,本地通话40分钟");
                String record = "本地通话40分钟";
                a.consumerMoney += (spend - a.pack.calltime) * 0.2;
                a.balance-=(spend-a.pack.calltime)*0.2;
                boolean isFirstRecord = addConsumptionRecord(a.number, record);
                if (isFirstRecord) {
                    System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
                }
            } else {
                a.pack.calltime = 0;
                int n = (int) (a.pack.calltime + 5 * a.balance);
                System.out.println("本次已通话" + n + "分钟,您的余额不足,请充值后再使用!");
                a.consumerMoney += a.balance;
                a.balance = 0;
            }
        }
        saveData();
    }

    public static void event6(Account a){
        int spend=600;
        if(a.pack.netflow-spend>=0){
            a.pack.netflow-=spend;
            System.out.println("和网友聊天,使用流量600mb");
            String record="使用流量600mb";
            boolean isFirstRecord = addConsumptionRecord(a.number, record);
            if (isFirstRecord) {
                System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
            }
        }else {
            if((a.pack.netflow+10*a.balance)-spend>0){
                a.pack.netflow=0;
                System.out.println("和网友聊天,使用流量600mb");
                String record="使用流量600mb";
                a.consumerMoney+=(spend-a.pack.netflow)*0.1;
                a.balance-=(spend-a.pack.netflow)*0.1;
                boolean isFirstRecord = addConsumptionRecord(a.number, record);
                if (isFirstRecord) {
                    System.out.println("不存在此卡的消费记录,已添加一条消费记录.");
                }
            }else {
                a.pack.netflow=0;
                int n=(int)((a.pack.netflow+10*a.balance));
                System.out.println("本次已上网"+n+"分钟,您的余额不足,请充值后再使用!");
                a.consumerMoney+= a.balance;
                a.balance=0;
            }
        }
        saveData();
    }
    public static void reg() throws IOException {
        String[] phones=randPhone();
        System.out.println("*****可选择的卡号*****");
        for(int i=0;i<phones.length;i++){
            System.out.print(i+1+"."+phones[i]+" ");
            if((i+1)%3==0) System.out.println();
        }
        System.out.println("请选择卡号(输入1~9的序号):");
        int n=0;
        String n1;
        while(true){
            n1=sc.next();
            if(n1.equals("1")||n1.equals("2")||n1.equals("3")||n1.equals("4")||n1.equals("5")||
                    n1.equals("6")||n1.equals("7")||n1.equals("8")||n1.equals("9")){
                n=Integer.parseInt(n1);
                break;
            }
            System.out.println("请输入正确的数字");
        }
        String phone=phones[n-1];
        System.out.println("1.话痨套餐 2.网虫套餐 3.超人套餐, 请选择套餐(输入序号):");
        int m=0;
        String m1;
        while(true){
            m1=sc.next();
            if(m1.equals("1")||m1.equals("2")||m1.equals("3")){
                m=Integer.parseInt(m1);
                break;
            }
            System.out.println("请输入正确的数字");
        }
        Package p=null;
        switch (m){
            case 1:p=new TallPackage(58,500,0,30); break;
            case 2:p=new NetPackage(68,0,3072,0); break;
            case 3:p=new SuperPackage(78,200,1024,50); break;
        }
        Package page=p;
        System.out.println("请输入用户姓名");
        String name=sc.next();
        System.out.println("请输入密码:");
        String password=sc.next();
        System.out.println("请输入预存的话费");
        double money=0;
        while(true) {
            money = sc.nextDouble();
            if(money<p.price){
                System.out.println("预存的话费不足以支付这个套餐,请重新充值");
            }else{
                break;
            }
        }
        Account account=new Account(phone,password,name,p,money-p.price,p.price);
        account.print();
        list.add(account);
        saveData();
    }
    public static void login(){
        try {
            System.out.println("请输入手机卡号:");
            String number=sc.next();
            System.out.println("请输入密码:");
            String password=sc.next();
            for(Account a:list){
                if(number.equals(a.number) &&password.equals(a.password))
                {
                    two(a); return;
                }
            }
        } catch (Exception e) {
            System.out.println("号码错误!");
        }
    }
    public static String[] randPhone(){
        Random r=new Random();
        String[] phones=new String[9];
        for(int i=0;i<phones.length;i++){
            StringBuilder sb=new StringBuilder("139");
            for(int j=0;j<8;j++){
                sb.append(r.nextInt(10));
            }
            phones[i]=sb.toString();
        }
        return phones;
    }
}

class Account implements Serializable{
    String number;
    String password;
    String name;
    Package pack;
    double balance;
    double consumerMoney;
    public Account(String number, String password, String name, Package pack, double balance, double consumerMoney) {
        this.number = number;
        this.password = password;
        this.name = name;
        this.pack = pack;
        this.balance = balance;
        this.consumerMoney = consumerMoney;
    }
    public void print(){
        DecimalFormat df=new DecimalFormat("#.#");
        System.out.println("注册成功! 卡号:"+number+"用户名:"+name+"当前余额"+df.format(balance));
        pack.print();
    }
}
abstract class Package implements Serializable { //套餐
    double price;
    double calltime;
    double netflow;
    double notenum;
    public abstract void print();
}
class TallPackage extends Package {//话痨
    public TallPackage(double price,double calltime,double netflow,double notenum){
        super.price=price;super.calltime=calltime;super.netflow=netflow;super.notenum=notenum;
    }
    public void print() {System.out.println("话痨套餐:通话时长为500分钟/月,短信条数为30条/月,上网流量为0GB/月");}
}
class SuperPackage extends Package {//超人
    public SuperPackage(double price,double calltime,double netflow,double notenum){
        super.price=price;super.calltime=calltime;super.netflow=netflow;super.notenum=notenum;
    }
    public void print() {
        System.out.println("超人套餐:通话时长为200分钟/月,短信条数为50条/月,上网流量为1GB/月");
    }
}
class NetPackage extends Package { //网虫
    public NetPackage(double price,double calltime,double netflow,double notenum){
        super.price=price;super.calltime=calltime;super.netflow=netflow;super.notenum=notenum;
    }
    public void print() {
        System.out.println("网虫套餐:通话时长为0分钟/月,短信条数为0条/月,上网流量为3GB/月");
    }
}
